package com.upf1sh.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.upf1sh.bean.AudioOrVideoEntity;
import com.upf1sh.mapper.AudioOrVideoMapper;
import lombok.val;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.UUID;

@Service
public class AudioOrVideoService {

    @Resource
    private AudioOrVideoMapper audioOrVideoMapper;

    public int insert(AudioOrVideoEntity audioOrVideo) {
        return audioOrVideoMapper.insert(audioOrVideo);
    }

    public List<AudioOrVideoEntity> selectAudio() {
        return audioOrVideoMapper.selectAllByType("audio");
    }

    public AudioOrVideoEntity selectAudioByPath(String path) {
        return audioOrVideoMapper.selectAudioByPath(path);
    }

    public List<AudioOrVideoEntity> selectVideo() {
        return audioOrVideoMapper.selectAllByType("video");
    }


    public Page<AudioOrVideoEntity> findAllAudioInDBByPage(Page<AudioOrVideoEntity> page) {
//      调用basemapper的分页查询方法
        QueryWrapper<AudioOrVideoEntity> wrapper = new QueryWrapper<AudioOrVideoEntity>()
                .eq("type", "audio");
        return audioOrVideoMapper.selectPage(page, wrapper);
    }

    public Page<AudioOrVideoEntity> findAllVideoInDBByPage(Page<AudioOrVideoEntity> page) {
//      调用basemapper的分页查询方法
        QueryWrapper<AudioOrVideoEntity> wrapper = new QueryWrapper<AudioOrVideoEntity>()
                .eq("type", "video");
        return audioOrVideoMapper.selectPage(page, wrapper);
    }

    public boolean saveVideo(MultipartFile file, String belong, String description) {
        val audioOrVideoEntity = new AudioOrVideoEntity();
        LocalDateTime date2 = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        String originalFilename = file.getOriginalFilename();
        String newFileName = UUID.randomUUID().toString().replace("-", "") + originalFilename;
        String dirPath = ("/data");
//        String dirPath = System.getProperty("user.dir");
        String path = "/" + "video" + "/" + newFileName;
        File destFile = new File(dirPath + path);
        audioOrVideoEntity.setPath(dirPath + path);
        audioOrVideoEntity.setType("video");
        audioOrVideoEntity.setBelong(belong);
        audioOrVideoEntity.setName(originalFilename);
        audioOrVideoEntity.setSize(file.getSize() / 1048576);
        audioOrVideoEntity.setTime(date2);
        audioOrVideoEntity.setDescription(description);

        if (!destFile.getParentFile().exists()) {
            destFile.getParentFile().mkdirs();
        }
        try {
            file.transferTo(destFile);
            insert(audioOrVideoEntity);
            return true;
        } catch (IOException e) {
            return false;
        }
    }


    public int updateWave(String wavepath, String path) {
        UpdateWrapper<AudioOrVideoEntity> wrapper = new UpdateWrapper<>();
        wrapper.set("waveformpath", wavepath); // 设置要更新的字段和值
        wrapper.eq("path", path); // 设置更新条件，这里以id字段等于1为例

        return audioOrVideoMapper.update(null, wrapper); // 调用Mapper接口的update方法执行更新操作
    }

    public boolean saveAudio(MultipartFile file, String belong, String description) throws UnsupportedAudioFileException, IOException {
        val audioOrVideoEntity = new AudioOrVideoEntity();
        LocalDateTime date2 = LocalDateTime.now(ZoneId.of("Asia/Shanghai"));
        String originalFilename = file.getOriginalFilename();
        String newFileName = UUID.randomUUID().toString().replace("-", "") + originalFilename;
        String dirPath = ("/data");
//        String dirPath = System.getProperty("user.dir");

        String path = "/" + "audio" + "/" + newFileName;
        File destFile = new File(dirPath + path);
        audioOrVideoEntity.setPath(dirPath + path);
        audioOrVideoEntity.setType("audio");
        audioOrVideoEntity.setBelong(belong);
        audioOrVideoEntity.setName(originalFilename);
        audioOrVideoEntity.setSize(file.getSize() / 1048576);
        audioOrVideoEntity.setTime(date2);
        audioOrVideoEntity.setDescription(description);

        if (!destFile.getParentFile().exists()) {
            destFile.getParentFile().mkdirs();
        }
        try {
            file.transferTo(destFile);
            insert(audioOrVideoEntity);
            return true;
        } catch (IOException e) {
            return false;
        }
    }


}
